class Solution {
public:
    void calc(string s, stack<int> &st) {
        char opt = s[0];
        int b = st.top();
        st.pop();
        int a = st.top();
        st.pop();
        switch (opt) {
            case '+': st.push(a + b); break;
            case '-': st.push(a - b); break;
            case '*': st.push(a * b); break;
            case '/': st.push(a / b); break;
        }
    }
    int evalRPN(vector<string>& tokens) {
        stack<int> st;
        for (int i = 0; i < tokens.size(); i++) {
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
                calc(tokens[i], st);
            } else {
                int num = 0, flag = 1;
                for (int j = 0; j < tokens[i].size(); j++) {
                    if (tokens[i][j] == '-') {
                        flag = -1;
                        continue;
                    }
                    num = num * 10 + (tokens[i][j] - '0');
                }
                st.push(num * flag);
            }
        }
        int result = st.top();
        st.pop();
        return result;
    }
};
